package mfund.com.controller;

import java.util.*;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.servlet.ServletContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import mfund.com.service.*;
import mfund.com.model.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
import javax.servlet.http.HttpSession; 



@Controller
public class ProductController
{
	@Resource
	private ProductService productService;

	@RequestMapping(value="getProducts",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody MFResult getProducts(HttpServletRequest request, 
		HttpServletResponse response,
		@RequestParam Integer pageNo,
		@RequestParam Integer pageSize)
	{

		HttpSession session = request.getSession(true);  
        Boolean flag = (Boolean)session.getAttribute("flag");
        if(flag==null){
        	session.setAttribute("flag", flag);
        }
        String sessionId=session.getId();

        System.out.println("flag:"+flag); 
        System.out.println(session);
        System.out.println("sessionId:"+sessionId);

		MFResult map=new MFResult();
		List<TProduct> products= productService.query();
		Integer totalRecord=products.size();
		Integer count=(pageNo * pageSize) > totalRecord ? totalRecord :(pageNo * pageSize) ;
		Integer start=(pageNo-1) * pageSize;

		List<TProduct> resultList=new ArrayList<TProduct>();
		for(int i=start;i<count;i++){
			resultList.add(products.get(i));
		}
		map.setData(resultList);
		map.setPageNo(pageNo);
		map.setPageSize(pageSize);
		map.setTotalCount(totalRecord);

		return map;
	}
}